const user=require('../models/user');
const md5=require('md5');
const toMd5=require('../utils/md5/md5');

var userModel=user.userModel;

//增加方法Add
async function addUser(user){
    try{
        user.password=toMd5.toMd5(user.password);
        user.cpwd=toMd5.toMd5(user.cpwd);
        if(user.password != user.cpwd){
            return {success:false,msg:'两次输入密码不相等'}
        }else{
            var user=await userModel.create(user);
            console.log(user);
            if(user=={}||user==undefined){
                return {success:false,msg:'用户保存失败'};
            }else{
                return {success:true,user:user};
            }
        }
    }catch (e){
        console.log('add user is error--error reason:'+e);
        return {success:false,msg:e};
    }
}


//查找 findByName
async  function findByName(name){
    try{
        var user=await userModel.findOne({name:name});
        if(user==undefined||user=={}){
            return {success:false,msg:'not found'};
        }else{
            return {success:true,user:user};
        }
    }catch (e){
        return {success:false,msg:e};
    }
}
exports.addUser=addUser;
exports.findByName=findByName;





